package com.txl.dao;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.txl.domain.ContactBean;
import com.txl.domain.ContactGroupBean;
import com.txl.domain.UserBean;
import com.txl.exception.DaoException;
import com.txl.pager.QueryResult;
import com.txl.utils.JdbcUtils;

public class ContactDao {

	QueryRunner runner = new QueryRunner(JdbcUtils.getDataSource());
	
	//查找联系人
	public ContactBean find(String contactid){
		try {
			String sql = "select * from contact where contactid=?";
			return (ContactBean) runner.query(sql, contactid, new BeanHandler(ContactBean.class));
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//根据指定的分组id来查找联系人
	public List<ContactBean> findByGroupID(String groupid) {
		try {
			String sql = "select * from contact where groupid=?";
			return (List<ContactBean>) runner.query(sql, groupid, new BeanListHandler(ContactBean.class));
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//添加联系人
	public void add(ContactBean contact){
		try {
			String sql = "insert into contact(contactid,contactname,phone,email,qq,groupid) values(?,?,?,?,?,?)";
			
			Object params[] ={contact.getContactid(), contact.getContactname(),contact.getPhone(),contact.getEmail(),contact.getQq(),contact.getGroupid()};
			runner.update(sql, params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//修改联系人
	public void update(ContactBean contact){
		try {
			String sql = "update contact set contactname=?,phone=?,email=?,qq=?,groupid=? where contactid=?";
			
			Object params[] ={contact.getContactname(),contact.getPhone(),contact.getEmail(),contact.getQq(),contact.getGroupid(),contact.getContactid()};
			runner.update(sql, params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//删除联系人
	public void delete(String contactid){
		try {
			String sql = "delete from contact where contactid=?";
			
			Object params[] ={contactid};
			runner.update(sql, params);
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//获取所有联系人
	public List<ContactBean> getAll(){
		try {
			String sql = "select * from contact";
			return (List<ContactBean>) runner.query(sql, new BeanListHandler(ContactBean.class));
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//获取页面数据和页面大小
	public QueryResult pageQuery(int startindex,int pagesize){
		QueryResult qr = new QueryResult();
		try{
			String sql = "select * from contact limit ?,?";
			Object params[] = {startindex,pagesize};
			List list = (List) runner.query(sql, params, new BeanListHandler(ContactBean.class));
			qr.setList(list);
			
			sql = "select count(*) from contact";
			params = new Object[]{};
			int totalrecord = ((Long)runner.query(sql, new ScalarHandler(1))).intValue();
			qr.setTotalrecord(totalrecord);
			return qr;
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//带有条件分页查找(局部分页查找)
	public QueryResult pageQuery(int startindex, int pagesize, String groupid) {
		QueryResult qr = new QueryResult();
		try{
			String sql = "select * from contact where groupid=? limit ?,?";
			Object params[] = {groupid,startindex,pagesize};
			List list = (List) runner.query(sql, params, new BeanListHandler(ContactBean.class));
			qr.setList(list);
			
			sql = "select count(*) from contact where groupid=?";
			int totalrecord = ((Long)runner.query(sql, groupid,new ScalarHandler(1))).intValue();
			qr.setTotalrecord(totalrecord);
			return qr;
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	//模糊查找
	public List<ContactBean> search(String key){
		try {
			String sql = "select * from contact where contactid like '%"+key+"%' or contactname like '%"+key+"%' or email like '%"+key+"%' or phone like '%"+key+"%' or qq like '%"+key+"%'";
			return (List<ContactBean>) runner.query(sql, new BeanListHandler(ContactBean.class));
		} catch (Exception e) {
			throw new DaoException(e);
		}
	}
}
